﻿package com.ufenlei.LTS{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.filters.DropShadowFilter;

	public class Window extends Sprite {
		private var father:Sprite;
		private var winZoomButton:ZoomButton;
		private var winCloseButton:CloseButton;
		private var _isMax:Boolean;
		private var _mainContent:Sprite;
		private var _windowWidth:int;
		private var _windowHeight:int;
		private var _windowX:int=0;
		private var _windowY:int=0;
		private var winBody:RoundRectBody;
		private var winHead:RoundRectHead;
		private var isOpened:Boolean;

		public function Window(fa:Sprite,winWidth:int=0,WinHeight:int=0,title:String=null,winVisible:Boolean=false) {
			trace("Window()");
			_isMax=true;
			father=fa;
			winBody=new RoundRectBody(winWidth,WinHeight-20,5,5);
			winHead=new RoundRectHead(winWidth,20,5,5,title);
			winZoomButton = new ZoomButton();
			winCloseButton = new CloseButton();
			winZoomButton.y=winZoomButton.height/5;
			winCloseButton.y=winCloseButton.height/5;
			winZoomButton.x=winBody.width-winZoomButton.width-winCloseButton.width-2*winCloseButton.width/5;
			winCloseButton.x=winHead.width-winCloseButton.width-winCloseButton.width/5;
			winBody.y+=winHead.height;
			this.addEventListener(MouseEvent.MOUSE_DOWN, goTop);
		    father.addEventListener(MouseEvent.CLICK, stopD);
			winZoomButton.addEventListener("Input_MOUSEUP",zoomWindow);
			winCloseButton.addEventListener(Input.CLICK,closeWindow);
			winHead.addEventListener(MouseEvent.MOUSE_DOWN, goWithMouse);
			winHead.addEventListener(MouseEvent.MOUSE_UP,stopGoWithMouse);
			this.addChild(winHead);
			this.addChild(winBody);
			this.addChild(winZoomButton);
			this.addChild(winCloseButton);
			fliter();
			if(winVisible){
				isOpened=true;
				father.addChild(this);
			}
		}
		public function setTitle(title:String){
			winHead.setTitle(title);
		}
		public function get windowY():int {
			return this._windowY;
		}
		public function get isMax():Boolean {
			return this._isMax;
		}
		public function maximize():void {
			winBody.scaleY=1;
			_isMax=true;
		}
		public function minimize():void {
			winBody.scaleY=0;
			_isMax=false;
		}
		public function addContent(child:Sprite) {
			winBody.addChild(child);
		}
		public function clearContent(child:Sprite) {
			winBody.removeChild(child);
		}
		public function appendContent() {
		}
		public function open() {
			if (isOpened==false) {
				var blackshadow:BlackShadow=new BlackShadow(father,winHead.headWidth,winHead.headHeight+winBody.bodyHeight,"open");
				blackshadow.x=this.x+winHead.headWidth/2;
				blackshadow.y=this.y;
				blackshadow.addEventListener("Shadow_OPEN", openWindow);
				isOpened=true;
			}
		}
		public function close() {
			if (isOpened==true) {
				father.removeChild(this);
				var blackshadow:BlackShadow=new BlackShadow(father,winHead.headWidth,winHead.headHeight+winBody.bodyHeight,"close");
				blackshadow.x=this.x+winHead.headWidth/2;
				blackshadow.y=this.y+(winHead.headHeight+winBody.bodyHeight)/2;
				isOpened=false;
			}
		}
		private function zoomWindow(evt:Event) {
			if (_isMax==true) {
				minimize();
			} else {
				maximize();
			}
		}
		private function closeWindow(evt:Event) {
			trace("closeWindow","isOpened", isOpened);
			if (isOpened==true) {
				father.removeChild(this);
				var blackshadow:BlackShadow=new BlackShadow(father,winHead.headWidth,winHead.headHeight+winBody.bodyHeight,"close");
				blackshadow.x=this.x+winHead.headWidth/2;
				blackshadow.y=this.y+(winHead.headHeight+winBody.bodyHeight)/2;
				isOpened=false;
			}
		}
		private function openWindow(evt:Event) {
			father.addChild(this);
		}
		private function goTop(evt:MouseEvent) {
			father.setChildIndex(this,father.numChildren-1);
		}
		private function goWithMouse(evt:MouseEvent) {
			winBody.alpha=0.5;
			this.startDrag();
		}
		private function stopGoWithMouse(evt:MouseEvent) {
			winBody.alpha=1;
			this.stopDrag();
		}
		private function fliter() {
			var shadow:DropShadowFilter = new DropShadowFilter();
			shadow.distance=4;
			shadow.angle=45;
			shadow.strength=0.5;
			this.filters=[shadow];
			var filtersArray:Array=new Array(shadow);
			this.filters=filtersArray;
		}
		private function stopD(evt:MouseEvent){
			this.stopDrag();
		}
	}
}
